package com.zy.service.bs.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zy.dao.bs.NoticeDao;
import com.zy.dao.bs.NoticeTypeDao;
import com.zy.entity.enums.StatusEnum;
import com.zy.entity.bs.Notice;
import com.zy.entity.bs.NoticeType;
import com.zy.pojo.exceptions.ServiceException;
import com.zy.pojo.query.bs.NoticeTypeQuery;
import com.zy.pojo.vos.common.PageVo;
import com.zy.service.base.impl.BaseServiceImpl;
import com.zy.service.bs.NoticeTypeService;
import com.zy.utils.DateUtil;

@Service("noticeTypeService")
public class NoticeTypeServiceImpl extends BaseServiceImpl<NoticeType> implements NoticeTypeService {

	@Autowired
	private NoticeTypeDao noticeTypeDao;
	@Autowired
	private NoticeDao noticeDao;

	@Override
	public PageVo pageNoticeType(NoticeTypeQuery noticeTypeQuery, Integer start, Integer limit) {
		return new PageVo(start, limit, this.noticeTypeDao.countNoticeType(noticeTypeQuery), this.noticeTypeDao.listNoticeType(noticeTypeQuery, start, limit));
	}

	@Override
	public void saveNoticeType(NoticeType noticeType) {
		long time = DateUtil.getTime();
		noticeType.setCreateTime(time);
		noticeType.setModifyTime(time);
		noticeType.setStatus(StatusEnum.NORMAL.getValue());
		this.noticeTypeDao.save(noticeType);
	}
	
	@Override
	public void updateNoticeType(NoticeType noticeType) {
		noticeType.setModifyTime(DateUtil.getTime());
		this.noticeTypeDao.updateByPrimaryKeySelective(noticeType);
	}

	@Override
	public void removeNoticeType(long id) {
		try {
			Notice notice = new Notice();
			notice.setType(id);
			List<Notice> notices = this.noticeDao.list(notice);
			if(!notices.isEmpty()){
				throw new ServiceException("在公告管理中，存在该类型的公告");
			}
			this.noticeTypeDao.removeByPrimaryKey(id);
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			NoticeType noticeType = new NoticeType();
			noticeType.setId(id);
			noticeType.setModifyTime(DateUtil.getTime());
			noticeType.setStatus(StatusEnum.DELETE.getValue());
			this.noticeTypeDao.updateByPrimaryKeySelective(noticeType);
		}
	}

	@Override
	public void recover(long id) {
		NoticeType noticeType = new NoticeType();
		noticeType.setId(id);
		noticeType.setModifyTime(DateUtil.getTime());
		noticeType.setStatus(StatusEnum.NORMAL.getValue());
		this.noticeTypeDao.updateByPrimaryKeySelective(noticeType);
	}

}
